package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.DtsGoodsProductMapper;
import com.qf.pojo.DtsGoodsProduct;
import com.qf.pojo.DtsGoodsSpecification;
import com.qf.service.GoodsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GoodsProductServiceImpl implements GoodsProductService {
    @Autowired
    private DtsGoodsProductMapper dtsGoodsProductMapper;
    @Override
    public Long findProductsCount() {
        return dtsGoodsProductMapper.selectCount(null);
    }

    @Override
    public List<DtsGoodsProduct> findListByGoodsId(Integer id) {
        return dtsGoodsProductMapper.selectList(new QueryWrapper<DtsGoodsProduct>()
                .eq("goods_id",id)
                .eq("deleted",false));
    }

    @Override
    public void update(DtsGoodsProduct[] products, Integer id) {
        dtsGoodsProductMapper.updateByGoodsId(id);
        if (products==null||products.length==0){
            return;
        }

        for (DtsGoodsProduct product : products) {
            product.setId(null);
            product.setUpdateTime(new Date());
            product.setDeleted(false);
            product.setAddTime(new Date());
            product.setGoodsId(id);
            dtsGoodsProductMapper.insert(product);
        }
    }

    @Override
    public void create(DtsGoodsProduct[] products, Integer goodsId) {
        if (products==null||products.length==0){
            return;
        }
        for (DtsGoodsProduct product : products) {
            product.setGoodsId(goodsId);
            product.setId(null);
            product.setAddTime(new Date());
            product.setUpdateTime(new Date());
            product.setDeleted(false);
            dtsGoodsProductMapper.insert(product);
        }



    }

}
